package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.ExportProductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.PackingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service
public class PackingServiceImpl implements PackingService {
    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private ExportProductDao exportProductDao;

    @Override
    public PageInfo findAll(int page, int size, PackingExample packingExample) {
        PageHelper.startPage(page,size);
        List<Packing> list = packingDao.selectByExample(packingExample);
        return new PageInfo(list);
    }

    @Override
    public Packing findById(String id) {
        return packingDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Packing> findAll(PackingExample example) {
        return packingDao.selectByExample(example);
    }

    //保存装箱单
    public Integer save(Packing packing, String id) { //id 报运单id[多个]  packing装箱单
        //先将报运单转化为数组
        String[] exportIds = id.split(",");

        //A判断报运单数据,如果就1个 直接保存装箱单,修改状态
        if (exportIds.length == 1) {
            String exportId = exportIds[0];

            //根据报运单id查询报运单
            Export export = exportDao.selectByPrimaryKey(exportId);
            packing.setExportIds(id);//报运单id集合
            packing.setExportNos(export.getCustomerContract());//报运合同号
            packing.setGrossWeights(BigDecimal.valueOf(export.getGrossWeights()));//毛重
            packing.setTotalVolume(BigDecimal.valueOf(export.getMeasurements()));//总体积
            //净重
            Double netWeights = 0.0;
            ExportProductExample example = new ExportProductExample();
            ExportProductExample.Criteria criteria = example.createCriteria();
            criteria.andExportIdEqualTo(exportId);
            List<ExportProduct> eps = exportProductDao.selectByExample(example);
            for (ExportProduct exportProduct : eps) {
                netWeights += exportProduct.getNetWeight();
            }
            packing.setNetWeights(BigDecimal.valueOf(netWeights));
            //生成装箱单id
            packing.setId(UUID.randomUUID().toString());
            packing.setState(0);
            //保存装箱单
            packingDao.insertSelective(packing);
        }

        //B 多个报运单一起装箱: 判断这些报运单的 装运港、目的港、收货人 是不是一致
        //取第一个报运单的数据用作比较 , 根据报运单id查询报运单

        String exportId = exportIds[0]; //获取第一个报运单id
        Export export = exportDao.selectByPrimaryKey(exportId);//获取第一个报运单对象

        String consignee = export.getConsignee(); //获取第一个报运单需要比较的 收货人
        String shipmentPort = export.getShipmentPort(); //获取第一个报运单需要比较的 装运港
        String destinationPort = export.getDestinationPort(); //获取第一个报运单需要比较的 目的港

        //从第二个开始[1]循环遍历,判断这三个属性值是否一致, 一致生成装箱单
        //tips: 第一个查出来作对比的报运单的数据也要添加进来
        //第一个报运单里需要被保存到装箱单中的数据
        String exportNos = export.getCustomerContract();//报运合同号们
        //净重
        Double netWeights = 0.0;
        //这里需要自己查询报运单商品 !!! 不能用报运单中实体类的报运货物集合(是为了方便修改,查不到数据的)
        ExportProductExample example = new ExportProductExample();
        ExportProductExample.Criteria criteria = example.createCriteria();
        criteria.andExportIdEqualTo(exportId);
        List<ExportProduct> eps = exportProductDao.selectByExample(example);
        for (ExportProduct ep : eps) {
            netWeights = ep.getNetWeight();
        }

        Double grossWeights = export.getGrossWeights();//毛重

        Double measurements = export.getMeasurements();//总体积

        //开始循环遍历剩下的报运单
        for (int i = 1; i < exportIds.length; i++) {
            String exportId1 = exportIds[i];//获取第i报运单id
            Export export1 = exportDao.selectByPrimaryKey(exportId1);//获取第i个报运单
            if (!export1.getConsignee().equals(consignee) ||
                    !export1.getShipmentPort().equals(shipmentPort) ||
                    !export1.getDestinationPort().equals(destinationPort)) {
                return 0;
            }
            //进入判断 装运港、目的港、收货人与第一个报运单的是不是一致
            //一致 累加需要保存到装箱中的数据
            exportNos += export1.getCustomerContract() + " ";//拼接合同号编号

            //净重
            ExportProductExample example1 = new ExportProductExample();
            ExportProductExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andExportIdEqualTo(exportId1);
            List<ExportProduct> eps1 = exportProductDao.selectByExample(example1);
            for (ExportProduct ep1 : eps1) {
                if (ep1.getNetWeight() != null) {
                    netWeights += ep1.getNetWeight();
                }

            }
            if (export1.getGrossWeights() != null) {
                //毛重
                grossWeights+=export1.getGrossWeights();
            }
            if (export1.getMeasurements() != null) {
                //总体积
                measurements+=export1.getMeasurements();
            }
        }
            //生成装箱单id
            packing.setId(UUID.randomUUID().toString());
            packing.setState(0);
            //保存数据
            packing.setExportIds(id);//报运单id集合
            packing.setExportNos(exportNos);//报运合同号
            packing.setGrossWeights(BigDecimal.valueOf(grossWeights));//毛重
            packing.setTotalVolume(BigDecimal.valueOf(measurements));//总体积
        if (netWeights != null) {
            packing.setNetWeights(BigDecimal.valueOf(netWeights)); //净重
        }
            //保存装箱单
            packingDao.insertSelective(packing);
            return 1;
    }

    @Override
    public void update(Packing packing) {

    }


    //删除装箱单
    public void delete(String id) {
        packingDao.deleteByPrimaryKey(id);
    }
}
